package de.koller.so.render.logo;

import de.koller.oceanFramework.gl.util.ImagePreview;
import de.koller.oceanFramework.math.Vec3;
import de.koller.oceanFramework.util.ColorIntUtil;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

public class LogoRenderer {
	public static final Random RND = new Random();
	public static final int W = 1000, H = 400;
	
	private static void fillHeightMap( HeightMap hm ) {
		BufferedImage gi = new BufferedImage( W, H, BufferedImage.TYPE_USHORT_GRAY );
		Graphics2D g = gi.createGraphics();
				
		g.setColor( Color.BLACK );
		g.fillRect( 0, 0, W+1, H+1 );
		
		g.scale( 30, 30 );
		g.setColor( Color.WHITE );
		g.setFont( new Font( Font.DIALOG, Font.BOLD, 10 ) );
		g.drawString( "Fabian", 1, 10 );
		
		g.dispose();
		ImagePreview.showImage( gi, W, H );
		
		ExecutorService s = Executors.newFixedThreadPool( 4 );
		for( int x = 0; x < hm.map.length; x++ ) {
			s.execute( new ExCalc( hm, x, gi ) );
		}
		
		try {
			s.shutdown();
			s.awaitTermination( 1, TimeUnit.DAYS );
		} catch ( InterruptedException ex ) {
			
		}
	}

	private static class ExCalc implements Runnable {

		private final HeightMap hm;
		private final int x;
		private final BufferedImage gi;

		public ExCalc( HeightMap hm, int x, BufferedImage gi ) {
			this.hm = hm;
			this.x = x;
			this.gi = gi;
		}
		
		@Override
		public void run() {
			final int trans = 15;
			
			for( int y = 0;  y < hm.map[0].length; y++ ) {
				double dis = Double.MAX_VALUE;
				for( int dx = -trans; dx < trans; dx++ )
					for( int dy = -trans; dy < trans; dy++ ) {
						try {
							boolean p = ColorIntUtil.getRValue( gi.getRGB( x+dx, y+dy ) ) > 0;
							if( p ) {
								double d = Point.distance( 0, 0, dx, dy );
								dis = Math.min( dis, d );
							}
						} catch( Exception ex ) {

						}
					}

				if( dis != Double.MAX_VALUE ) {
					dis = Math.sqrt( dis );
					//hm.map[x][y] = Math.cos( Math.atan( dis / trans ) * trans ) * 3.5;
					hm.map[x][y] = -dis;
					//hm.map[x][y] = Math.max( 0, hm.map[x][y] );
				}
			}
			System.out.println( x );
		}
		
	}
	
	private static void colorLight( LightMap lm ) {
		for( int x = 0; x < lm.basicColor.length; x++ )
			for( int y = 0;  y < lm.basicColor[0].length; y++ ) {
				lm.basicColor[x][y] = ColorIntUtil.getIntColor( 0xff, 0xff, 0, 0 );
			}
	}
	
	
	public static void main( String[] args ) {
		HeightMap h = new HeightMap( W, H );
		
		fillHeightMap( h );
		ImagePreview.showImage( h.getVisual(), W, H );
		
		LightMap l = new LightMap( new Vec3( -1.5, 1, 2 ), h );
		
		colorLight( l );
		l.basicColor();
		ImagePreview.showImage( l.getVisual(), W, H );
		
		l.calcBasicLight();
		ImagePreview.showImage( l.getVisual(), W, H );
	}
	
}
